<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isFullScreen ? 'exit-fullscreen':'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
import screenfull from 'screenfull'
// 插件实现
// 网址 https://github.com/sindresorhus/screenfull.js#readme
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false
    }
  },

  mounted() {
    // window.addEventListener('fullscreenchange', () => {
    //   // document.fullscreenElement是否全屏,不是全屏值为null
    //   const isFull = document.fullscreenElement
    //   if (isFull === null) {
    //     this.isFullScreen = false
    //   }
    // })
    // 这里有一个on方法 监控事件的触发 在事件触发的回调中让我们控制icon的状态和是否全屏保持一致
    screenfull.on('change', () => {
      // 把自己用来控制图标显示的isFullscreen和人家给到的是否全屏保持一致
      this.isFullScreen = screenfull.isFullscreen
    })
  },
  methods: {
    toggleScreen() {
      // if (this.isFullScreen) {
      //   // 退出全屏
      //   document.exitFullscreen()
      // } else {
      //   // 全屏
      //   document.documentElement.requestFullscreen()
      // }
      // this.isFullScreen = !this.isFullScreen
      // 来回的取反操作
      screenfull.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
